<?php

/* 微公众平台关键词自动回复管理控制器 */

class My_wxmessApp extends BackendApp {

    var $_store_id;
    var $my_wxkeyword_mod;
    var $wxfile_mod;

    function __construct() {
        $this->My_wxmess();
    }

    function My_wxmess() {
        parent::__construct();
        $this->my_wxkeyword_mod = & m('wxkeyword');
        $this->wxfile_mod = & m('wxfile');
        $this->_store_id = '0';
        
        $this->_payment_wx_appid = Conf::get('payment_wx_appid');
        $this->_payment_wx_secret = Conf::get('payment_wx_secret');
        if(empty($this->_payment_wx_appid)||empty($this->_payment_wx_secret)){
            $this->show_message(Lang::get('no_ininterface'), Lang::get('Please_configure'), 'index.php?app=my_wxconfig');
            exit;
        }
    }

    function index() {
        /* 当前位置 */
        
        $this->display('my_wxmess.index.html');
    }

    function showmess() {
        $conditions = ' AND ismess=1 AND user_id=' . $this->_store_id;
        $keyinfo = $this->my_wxkeyword_mod->find(array(
            'conditions' => '1=1 ' . $conditions,
        ));
        if (is_array($keyinfo)) {
            foreach ($keyinfo as $k => $v) {
                $titles = unserialize($v['titles']);
                $imageinfo = unserialize($v['imageinfo']);
                $linkinfo = unserialize($v['linkinfo']);
                $keyinfo[$k]['titles'] = $titles;
                $keyinfo[$k]['imageinfo'] = $imageinfo;
                $keyinfo[$k]['linkinfo'] = $linkinfo;
            }
        }

        die(json_encode($keyinfo));
    }

    function del() {
        if (IS_POST) {
            $kid = isset($_POST['kid']) && !empty($_POST['kid']) ? intval($_POST['kid']) : 0;
            if ($kid > 0) {
                $this->my_wxkeyword_mod->drop($kid);
            }
        }
    }

    function save() {
        if (IS_POST) {
            $kid = $this->my_wxkeyword_mod->get_mess_id();
            $kid = empty($kid) ? 0 : $kid;
            $ketype = isset($_POST['ketype']) && !empty($_POST['ketype']) ? intval($_POST['ketype']) : 0;
            if ($ketype == 1) {

                $kecontent = isset($_POST['kecontent']) && !empty($_POST['kecontent']) ? $_POST['kecontent'] : '';
                $data = array(
                    'type' => $ketype,
                    'kecontent' => $kecontent,
                    'linkinfo' => '',
                    'titles' => '',
                    'imageinfo' => '',
                    'ismess' => 1,
                    'user_id' => $this->_store_id
                );
                if ($kid) {
                    $this->my_wxkeyword_mod->edit($kid, $data);
                } else {
                    $this->my_wxkeyword_mod->add($data);
                }
            } elseif ($ketype == 2) {

                $titles = isset($_POST['titles']) && !empty($_POST['titles']) ? $_POST['titles'] : '';
                $imageinfo = isset($_POST['imageinfo']) && !empty($_POST['imageinfo']) ? $_POST['imageinfo'] : '';
                $linkinfo = isset($_POST['linkinfo']) && !empty($_POST['linkinfo']) ? $_POST['linkinfo'] : '';
                $data = array(
                    'type' => $ketype,
                    'kecontent' => '',
                    'linkinfo' => serialize($linkinfo),
                    'titles' => serialize($titles),
                    'imageinfo' => serialize($imageinfo),
                    'ismess' => 1,
                    'user_id' => $this->_store_id
                );
                if ($kid) {
                    $this->my_wxkeyword_mod->edit($kid, $data);
                } else {
                    $this->my_wxkeyword_mod->add($data);
                }
            }
        }
    }

    /**
     *    三级菜单
     *
     *    @author    Hyber
     *    @return    void
     */
    function _get_member_submenu() {
        $submenus = array(
            array(
                'name' => 'my_wxmess',
                'url' => 'index.php?app=my_wxkeyword',
            ),
        );
        return $submenus;
    }

    function allimages() {
        $conditions = ' AND user_id=' . $this->_store_id;
        $allimages = $this->wxfile_mod->findAll(array(
            'order' => "file_id desc",
            'fields' => 'file_id as iid, file_path as imgurl',
            'conditions' => '1=1 ' . $conditions,
            'count' => true)
        );
        die(json_encode(array_values($allimages)));
    }

    function ajaxupload() {
        $file = $_FILES['image'];
        $file_path = $this->uploadfile($file);
        $file_type = $this->_return_mimetype($file_path);
        /* 文件入库 */
        $data = array(
            'file_type' => $file_type,
            'user_id' => $this->_store_id,
            'file_size' => $file['size'],
            'file_name' => $file['name'],
            'file_path' => $file_path
        );
        $this->wxfile_mod->add($data);
    }

    function delimage() {
        $file_id = isset($_POST['iid']) && !empty($_POST['iid']) ? intval($_POST['iid']) : 0;
        if ($file_id == 0) {
            $this->json_error('no_post_params_authorize');
            exit();
        }
        $this->wxfile_mod->drop($file_id);
    }
    
    
    /**
     * ECMALL上传文件
     */
    function uploadfile($para) {
        import('image.func');
        import('uploader.lib');
        $uploader = new Uploader();
        $uploader->allowed_type(IMAGE_FILE_TYPE);
        $uploader->allowed_size(2097152); // 2M
        $files = $para; //$_FILES['activity_banner'];
        if ($files['error'] == UPLOAD_ERR_OK) {
            /* 处理文件上传 */
            $file = array(
                'name' => $files['name'],
                'type' => $files['type'],
                'tmp_name' => $files['tmp_name'],
                'size' => $files['size'],
                'error' => $files['error']
            );
            $uploader->addFile($file);
            if (!$uploader->file_info()) {
                $data = current($uploader->get_error());
                $res = Lang::get($data['msg']);
                $this->view_iframe();
                echo "<script type='text/javascript'>alert('{$res}');</script>";
                return false;
            }

            $uploader->root_dir(ROOT_PATH);
            $dirname = 'data/files/mall/weixin';
            $filename = $uploader->random_filename();
            $file_path = $uploader->save($dirname, $filename);
        }
        return $file_path;
    }
    
    /**
     * 返回文件类型
     */
    function _return_mimetype($filename) {
        preg_match("|\.([a-z0-9]{2,4})$|i", $filename, $fileSuffix);
        switch (strtolower($fileSuffix[1])) {
            case "js" :
                return "application/x-javascript";

            case "json" :
                return "application/json";

            case "jpg" :
            case "jpeg" :
            case "jpe" :
                return "image/jpeg";

            case "png" :
            case "gif" :
            case "bmp" :
            case "tiff" :
                return "image/" . strtolower($fileSuffix[1]);

            case "css" :
                return "text/css";

            case "xml" :
                return "application/xml";

            case "doc" :
            case "docx" :
                return "application/msword";

            case "xls" :
            case "xlt" :
            case "xlm" :
            case "xld" :
            case "xla" :
            case "xlc" :
            case "xlw" :
            case "xll" :
                return "application/vnd.ms-excel";

            case "ppt" :
            case "pps" :
                return "application/vnd.ms-powerpoint";

            case "rtf" :
                return "application/rtf";

            case "pdf" :
                return "application/pdf";

            case "html" :
            case "htm" :
            case "php" :
                return "text/html";

            case "txt" :
                return "text/plain";

            case "mpeg" :
            case "mpg" :
            case "mpe" :
                return "video/mpeg";

            case "mp3" :
                return "audio/mpeg3";

            case "wav" :
                return "audio/wav";

            case "aiff" :
            case "aif" :
                return "audio/aiff";

            case "avi" :
                return "video/msvideo";

            case "wmv" :
                return "video/x-ms-wmv";

            case "mov" :
                return "video/quicktime";

            case "rar" :
                return "application/x-rar-compressed";

            case "zip" :
                return "application/zip";

            case "tar" :
                return "application/x-tar";

            case "swf" :
                return "application/x-shockwave-flash";

            default :
                if (function_exists("mime_content_type")) {
                    $fileSuffix = mime_content_type($filename);
                }
                return "unknown/" . trim($fileSuffix[0], ".");
        }
    }

}

?>